<template>
	<gui-page>
		<template v-slot:gBody>
			<view class="gui-padding gui-bg-white gui-dark-bg-level-3">
				<gui-schedule :schedules="schedules" :currentDate="nowDate" @scheduleTap="scheduleTap"
					@chooseDate="chooseDate" @selectDate="selectDate" v-if="schedules != null"></gui-schedule>
			</view>
			<view class="gui-padding">
				<button class="gui-button gui-bg-green" type="default" @click="removeDate" v-if="flag">
					<text class="gui-block gui-button-text gui-color-white">设置为接单</text>
				</button>
				<button class="gui-button gui-bg-red" type="default" @click="addDate" v-else>
					<text class="gui-block gui-button-text gui-color-white">设置为不接单</text>
				</button>
			</view>
		</template>
	</gui-page>
</template>
<script>
	export default {
		data() {
			return {
				schedules: null,
				nowDate: '2024-03-06',
				flag: false,
				choose_date: '',
				shop_id: "",
				userInfo: '',
			}
		},
		onShow() {
			var user = uni.gRequest.checkLogin();
			console.log(user)
			if (user) {
				this.userInfo = user;
			}
		},
		onLoad(e) {
			if (e) {
				this.shop_id = e.shop_id;
			}
			// 模拟api加载数据
			setTimeout(() => {
				this.initData();
				// this.schedules=[{
				// 	datetime: '2024-03-06',
				// 	content: '不接单',
				// 	bgColor: '#FF0036',
				// 	color: '#FFFFFF',
				// }]
			}, 1000);

			let today = new Date();
			let year = today.getFullYear();
			let month = today.getMonth() + 1; // 月份从0开始计算，需要加1
			let day = today.getDate();
			this.nowDate = year + '-' + String(month).padStart(2, '0') + '-' + String(day).padStart(2, '0');
		},
		methods: {
			initData() {
				uni.gRequest.get("rest/list", {
					shopId: this.shop_id,
					userId: this.userInfo.userId
				}).then(result => {
					this.schedules = result.rows;
				});
			},
			scheduleTap(id) {
				uni.showToast({
					title: "id " + id,
					icon: "none"
				});
			},
			chooseDate: function(e) {
				console.log("客户选择了日期 : " + e);
				this.choose_date = e;
				
				let c = false;
				
				var self = this;
				self.schedules.forEach(x => {
					if (x.datetime.indexOf(e) !== -1) {
						c = true;
					}
				})
				self.flag = c
			},
			selectDate(e) {
				console.log("客户从日期picker选择日期 : " + e);
			},
			addDate() {
				
				var now = new Date().getTime();
				var choose = new Date(this.choose_date).getTime();
				if (now > choose) {
					uni.showToast({
						title: '选择日期必须超过当前日期',
						icon: 'none'
					})
					return false;
				}

				var formData = {
					datetime: this.choose_date,
					content: '不接单',
					bgColor: '#FF0036',
					color: '#FFFFFF',
					shopId: this.shop_id,
					userId: this.userInfo.userId,
				}
				uni.gRequest.post("rest", JSON.stringify(formData)).then(x => {
					uni.showToast({
						title: '设置成功'
					})
					// this.initData();
					// this.schedules.push(formData);
					this.$router.go(0);
				});

			},
			removeDate(){
				uni.gRequest.get("rest/remove/rest", {
					datetime:this.choose_date,
					shop_id: this.shop_id,
					user_id: this.userInfo.userId
				}).then(result => {
					uni.showToast({
						title: '设置成功'
					})
					this.$router.go(0);
				});
			}
		}
	}
</script>
<style scoped>
</style>